package com.qianxun.module.datasource1.common.rest;

import com.qianxun.module.base.constant.Result;
import com.qianxun.module.datasource1.common.manager.CaptchaValidObjectHolder;
import com.wf.captcha.*;
import com.wf.captcha.base.Captcha;
import com.wf.captcha.utils.CaptchaUtil;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.security.SecureRandom;
import java.util.List;

/**
 * 验证码
 * @author: huwei
 * @date: 2019/9/18 15:42
 * @version: 1.0.0
 */
@RestController
@RequestMapping("/captcha")
public class CaptchaRestController {

    /**
     * 流式输出
     * @param request 请求体
     * @param response 返回体
     * @param width 验证码宽度，默认138px
     * @param height 验证码高度，默认48px
     * @throws IOException
     */
    @RequestMapping("/stream")
    public void stream(HttpServletRequest request , HttpServletResponse response
            ,@RequestParam(required = false ,defaultValue = "130") Integer width
            ,@RequestParam(required = false ,defaultValue = "48") Integer height) throws IOException {
        CaptchaUtil.out(randomCaptcha(width ,height) ,request ,response);
    }

    /**
     * 返回验证码的base64码
     * @param request 请求体
     * @param width 验证码宽度，默认138px
     * @param height 验证码高度，默认48px
     * @return
     * @throws IOException
     */
    @RequestMapping("/base64")
    public Result base64(HttpServletRequest request
            ,@RequestParam(required = false ,defaultValue = "130") Integer width
            ,@RequestParam(required = false ,defaultValue = "48") Integer height) throws IOException {
        Captcha captcha = randomCaptcha(width ,height);
        request.getSession().setAttribute("captcha", captcha.text().toLowerCase());
        return Result.success(captcha.toBase64());
    }

    /**
     * 添加指定名称对象的验证码验证状态
     * @param validCode 验证对象编码，即CaptchaValidObjectEnum中定义对象code码
     * @return
     */
    @PostMapping(value = {"/add/valid"})
    public Result addValid(@RequestParam(value = "validCode") String validCode){
        CaptchaValidObjectHolder.getInstance().add(validCode);
        return Result.success();
    }

    /**
     * 清除指定名称对象的验证码验证状态
     * @param validCode 验证对象编码，即CaptchaValidObjectEnum中定义对象code码
     * @return
     */
    @PostMapping(value = {"/cancel/valid"})
    public Result cancelValid(String validCode){
        CaptchaValidObjectHolder.getInstance().cancel(validCode);
        return Result.success();
    }

    @RequestMapping(value = {"/search/all"})
    public Result searchAll(){
        List<CaptchaValidObjectHolder.CaptchaObjectStatus> statusList =  CaptchaValidObjectHolder.getInstance().getAll();
        return Result.success(statusList);
    }

    /**
     * 随机获取一种类型的验证码
     * @param width 宽度
     * @param height 高度
     * @return
     */
    private Captcha randomCaptcha(int width ,int height){
        Captcha captcha;
        int random = new SecureRandom().nextInt(5);
        switch (random){
            case 0:
                captcha = new SpecCaptcha(width ,height);
                break;
            case 1:
                captcha = new ArithmeticCaptcha(width ,height);
                break;
            default:
                captcha = new GifCaptcha(width ,height);
        }
        if(captcha instanceof ArithmeticCaptcha){
            captcha.setLen(2);
        }else{
            captcha.setLen(4);
        }
        return captcha;
    }
}
